package sale_admin

import (
	"context"
	"sale-admin/internal/app/grpc/protoc/sale_adminc"
	"sale-admin/internal/dal"
	"sale-admin/internal/dao"
	"sale-admin/internal/define"

	"192.168.1.75/go-pkg/errorx"
	"github.com/samber/lo"
)

// 机器商品列表
func (s SaleAdminService) RobotGoodsList(ctx context.Context, req *sale_adminc.RobotGoodsListReq) (*sale_adminc.RobotGoodsListRsp, error) {
	robotGoodsList := []*sale_adminc.RobotGoodsListRsp_RobotGoodsList{}
	// 获取机器商品列表
	list := dao.Goods{}.RobotGoodsList(
		ctx,
		dal.Q,
		req.RobotId,
		int(req.GoodsType), 0, req.GoodsId, req.GoodsName,
		int(req.ContainerUseStatus), int(req.InventoryStatus),
	)
	// 排除下架的商品
	// 下架的商品
	offShelfGoodsIDs, _ := dao.ObjAssociation{}.GetAssociationID(
		context.Background(),
		dal.Q,
		define.AssociationRobotGoodsOffShelf,
		"",
		req.RobotId,
	)

	for _, record := range list {
		if !lo.Contains(offShelfGoodsIDs, record.GoodsID) {
			robotGoodsList = append(robotGoodsList, &sale_adminc.RobotGoodsListRsp_RobotGoodsList{
				GoodsId:        record.GoodsID,
				GoodsName:      record.GoodsName,
				GoodsImage:     record.GoodsImage,
				RecommendImage: record.RecommendImage,
				AdImage:        record.AdImage,
				GoodsPrice:     record.GoodsPrice,
				GoodsType:      int32(record.GoodsType),
				GoodsInventory: int32(record.GoodsInventory - record.GoodsReserveInventory),
			})
		}
	}
	rsp := &sale_adminc.RobotGoodsListRsp{}
	rsp.List = robotGoodsList
	return rsp, nil
}

// GoodsDetail 商品详情
func (s SaleAdminService) GoodsDetail(ctx context.Context, req *sale_adminc.GoodsDetailReq) (*sale_adminc.GoodsDetailRsp, error) {
	detail, _ := dao.Goods{}.Detail(ctx, dal.Q, req.GoodsId)
	if detail == nil {
		return nil, errorx.New("商品不存在", -1)
	}
	rsp := &sale_adminc.GoodsDetailRsp{}
	rsp.GoodsId = detail.GoodsID
	rsp.GoodsName = detail.GoodsName1
	rsp.GoodsImage = detail.GoodsImage
	rsp.GoodsPrice = detail.Price
	rsp.GoodsType = int32(detail.GoodsType)
	rsp.RecommendImage = detail.RecommendImage
	rsp.AdImage = detail.AdImage
	rsp.Detail = detail.GoodsDetail
	rsp.ReviewPoints = int32(detail.ReviewPoints)

	rsp.GoodsGuideType = string(detail.GoodsGuideType)
	rsp.GoodsGuideUrl = detail.GoodsGuideURL
	rsp.ShareCodeType = string(detail.ShareCodeType)
	rsp.ShareCode = detail.ShareCode
	rsp.GoodsGuideNotice = detail.GoodsGuideNotice
	return rsp, nil
}

// GoodsTagList 商品标签
func (s SaleAdminService) GoodsTagList(ctx context.Context, req *sale_adminc.GoodsTagListReq) (*sale_adminc.GoodsTagListRsp, error) {
	rsp := &sale_adminc.GoodsTagListRsp{}
	rsp.List = []*sale_adminc.GoodsTagListRsp_GoodsTag{}
	list, _, _ := dao.GoodsClass{}.List(ctx, dal.Q, "", "", -1, -1)
	for _, v := range list {
		rsp.List = append(rsp.List, &sale_adminc.GoodsTagListRsp_GoodsTag{
			Id:   v.ID,
			Name: v.ClassName,
		})
	}
	return rsp, nil
}

// GoodsCornerLableList 商品脚标列表
func (s SaleAdminService) GoodsCornerLableList(ctx context.Context, req *sale_adminc.GoodsCornerLabelListReq) (*sale_adminc.GoodsCornerLabelListRsp, error) {
	rsp := &sale_adminc.GoodsCornerLabelListRsp{}
	rsp.List = []*sale_adminc.GoodsCornerLabelListRsp_CornerLabel{}
	list, _ := dao.GoodsCornerLabel{}.List(ctx, dal.Q, "", req.Name, -1, -1)
	for _, record := range list {
		rsp.List = append(rsp.List, &sale_adminc.GoodsCornerLabelListRsp_CornerLabel{
			Id:       record.ID,
			Name:     record.Name,
			Position: int32(record.Position),
			Image:    record.Image,
		})
	}
	return rsp, nil
}
